package com.js.rtu.fragment.second;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

import com.js.rtu.R;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.config.CmdConfig;
import com.js.rtu.config.IniConfig;
import com.js.rtu.util.BluetoothUtils;
import com.js.rtu.util.CmdUtils;
import com.js.rtu.util.ToastUtil;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 22_10多段自报间隔
 * 第 1 个字节： 多段数量 N（最多可设置 4 段）
 * 第 2-6 字节： 2 字节起始“时分” +2 字节结束“时分” +1 字节自报间隔
 * 第 3-第 2*（N-1） -6*（N-1） 字节： 2 字节起始“时分” +2 字节结束“时分” +1 字节自报间隔
 * 注意：（1） 自报间隔仅支持 3 \ 5 \ 10 \ 15 \ 20 \ 30 分钟
 * （2） “时” 采用 24 小时制式， 00:00-23:59
 * Created by zwz on 2020/6/22.
 */
public class Cmd_22_0BFragment extends BaseFragment {



    @BindView(R.id.radio)
    RadioGroup radio;
    @BindView(R.id.edt_start1)
    EditText edtStart1;
    @BindView(R.id.edt_end1)
    EditText edtEnd1;
    @BindView(R.id.sp_1)
    Spinner sp1;
    @BindView(R.id.ll_01)
    LinearLayout ll01;
    @BindView(R.id.edt_start2)
    EditText edtStart2;
    @BindView(R.id.edt_end2)
    EditText edtEnd2;
    @BindView(R.id.sp_2)
    Spinner sp2;
    @BindView(R.id.ll_02)
    LinearLayout ll02;
    @BindView(R.id.edt_start3)
    EditText edtStart3;
    @BindView(R.id.edt_end3)
    EditText edtEnd3;
    @BindView(R.id.sp_3)
    Spinner sp3;
    @BindView(R.id.ll_03)
    LinearLayout ll03;
    @BindView(R.id.edt_start4)
    EditText edtStart4;
    @BindView(R.id.edt_end4)
    EditText edtEnd4;
    @BindView(R.id.sp_4)
    Spinner sp4;
    @BindView(R.id.ll_04)
    LinearLayout ll04;
    @BindView(R.id.rbtn_1)
    RadioButton rbtn1;
    @BindView(R.id.rbtn_2)
    RadioButton rbtn2;
    @BindView(R.id.rbtn_3)
    RadioButton rbtn3;
    @BindView(R.id.rbtn_4)
    RadioButton rbtn4;
    private int total = 1;

    public static Cmd_22_0BFragment newInstance() {
        Cmd_22_0BFragment fragment = new Cmd_22_0BFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_second_cmd_22_0b;
    }

    @Override
    protected void initView() {

        ll02.setVisibility(View.GONE);
        ll03.setVisibility(View.GONE);
        ll04.setVisibility(View.GONE);
        radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbtn_1:
                        total = 1;
                        ll01.setVisibility(View.VISIBLE);
                        ll02.setVisibility(View.GONE);
                        ll03.setVisibility(View.GONE);
                        ll04.setVisibility(View.GONE);
                        break;
                    case R.id.rbtn_2:
                        total = 2;
                        ll01.setVisibility(View.VISIBLE);
                        ll02.setVisibility(View.VISIBLE);
                        ll03.setVisibility(View.GONE);
                        ll04.setVisibility(View.GONE);
                        break;
                    case R.id.rbtn_3:
                        total = 3;
                        ll01.setVisibility(View.VISIBLE);
                        ll02.setVisibility(View.VISIBLE);
                        ll03.setVisibility(View.VISIBLE);
                        ll04.setVisibility(View.GONE);
                        break;
                    case R.id.rbtn_4:
                        total = 4;
                        ll01.setVisibility(View.VISIBLE);
                        ll02.setVisibility(View.VISIBLE);
                        ll03.setVisibility(View.VISIBLE);
                        ll04.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });


        ArrayAdapter<String> mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, IniConfig.fenduanzibaojiange_des);
        sp1.setAdapter(mAdapter);
        sp2.setAdapter(mAdapter);
        sp3.setAdapter(mAdapter);
        sp4.setAdapter(mAdapter);


    }


    @OnClick({ R.id.btn_set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.btn_get:
//                get92Info(tvInfo);
//                break;
            case R.id.btn_set:
                set();
                break;
        }
    }

    private void set() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(CmdConfig.CMD_22_0B).append(" ")
                .append(String.format(Locale.CHINA, "%02x", total)).append(" ");
        if (rbtn1.isChecked()) {
            getInput(stringBuilder, edtStart1, edtEnd1, sp1);
        } else if (rbtn2.isChecked()) {
            getInput(stringBuilder, edtStart1, edtEnd1, sp1);
            getInput(stringBuilder, edtStart2, edtEnd2, sp2);
        } else if (rbtn3.isChecked()) {
            getInput(stringBuilder, edtStart1, edtEnd1, sp1);
            getInput(stringBuilder, edtStart2, edtEnd2, sp2);
            getInput(stringBuilder, edtStart3, edtEnd3, sp3);
        } else {
            getInput(stringBuilder, edtStart1, edtEnd1, sp1);
            getInput(stringBuilder, edtStart2, edtEnd2, sp2);
            getInput(stringBuilder, edtStart3, edtEnd3, sp3);
            getInput(stringBuilder, edtStart4, edtEnd4, sp4);
        }

        sendData(stringBuilder.substring(0, stringBuilder.length() - 1));
    }

    private void getInput(StringBuilder sb, EditText start, EditText end, Spinner sp) {
        String str_start = start.getText().toString();
        String str_end = end.getText().toString();
        if (str_start.isEmpty() || str_end.isEmpty()) {
            toastShow("请填写参数");
            return;
        }
        if (!str_start.contains(":") || !str_end.contains(":")) {
            toastShow("数据格式有误");
            return;
        }
        String[] data1 = str_start.split(":");
        String[] data2 = str_end.split(":");
        String interval = String.format(Locale.CHINA, "%02x",
                Integer.parseInt(IniConfig.fenduanzibaojiange_data.get(sp.getSelectedItemPosition())));

        sb.append(String.format(Locale.CHINA, "%02x", Integer.parseInt(data1[0]))).append(" ")
                .append(String.format(Locale.CHINA, "%02x", Integer.parseInt(data1[1]))).append(" ")
                .append(String.format(Locale.CHINA, "%02x", Integer.parseInt(data2[0]))).append(" ")
                .append(String.format(Locale.CHINA, "%02x", Integer.parseInt(data2[1]))).append(" ")
                .append(interval).append(" ");//间隔
    }


    private void sendData(String data) {
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_22, data);

        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                dismissLoadingDialog();

                String[] data = msg.split(" ");
                if (data.length < 18) {
                    ToastUtil.info(msg);
                    return;
                }
                if (data[12].equals(CmdConfig.CMD_22)
                        && data[13].equals(CmdConfig.CMD_22_0B)) {
                    if (data[14].equals(CmdConfig.SUCCECC)) {
                        ToastUtil.success("设置成功");
                    } else {
                        ToastUtil.error("设置失败");
                    }

                }


            }

            @Override
            public void onFail(String err) {
                toastShow(err);
                dismissLoadingDialog();
            }
        });
    }




}
